﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Collections.ObjectModel;
using System.Data;

namespace Portal.ExamQuestion.Converter
{
    public class WordToXmlConverter
    {
        public static void Execute(string SrcFile, string DestFile)
        {
            Microsoft.Office.Interop.Word.Application 
                WordDocument = new Microsoft.Office.Interop.Word.Application();
            WordDocument.Documents.Open(SrcFile);
            WordDocument.Application.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView;
            WordDocument.Visible = false;
            WordDocument.Application.Selection.HomeKey(Microsoft.Office.Interop.Word.WdUnits.wdStory);
            WordDocument.Application.Selection.Cells[1].Select();
            try
            {
                Collection<QuestionCls> Questions = new Collection<QuestionCls> { };
                QuestionCls CurrentQuestion = null;
                while (true)
                {
                    bool CheckStart = false;

                    string cText1 = WordDocument.Application.Selection.Text;
                    cText1 = cText1.Replace("\r\a", "");
                    if (cText1.ToLower().Equals("end"))
                    {
                        CheckStart = true;
                        WordDocument.Application.Selection.Cells[1].Next.Select();
                        try
                        {
                            WordDocument.Application.Selection.Cells[1].Next.Select();
                        }
                        catch
                        {
                            break;
                        }
                        cText1 = WordDocument.Application.Selection.Text;
                        cText1 = cText1.Replace("\r\a", "");
                    }
                    else
                    {
                        try
                        {
                            int.Parse(cText1);
                            CheckStart = true;
                        }
                        catch { }
                    }
                    WordDocument.Application.Selection.Cells[1].Next.Select();
                    string cText2 = WordDocument.Application.Selection.Text;
                    cText2 = cText2.Replace("\r\a", "");
                    WordDocument.Application.Selection.Cells[1].Next.Select();
                    if (CheckStart)
                    {
                        CurrentQuestion = new QuestionCls();
                        CurrentQuestion.QuestionId = System.Guid.NewGuid().ToString();
                        CurrentQuestion.QuestionNo = cText1;
                        CurrentQuestion.Subject = cText2;
                        CheckStart = false;
                        Questions.Add(CurrentQuestion);
                    }
                    else
                    {
                        AnswerItemCls OAnswerItem = new AnswerItemCls();
                        OAnswerItem.AnswerId = System.Guid.NewGuid().ToString();
                        OAnswerItem.AnswerText = cText2;
                        OAnswerItem.IsAnswer = cText1.Equals("*");
                        OAnswerItem.QuestionId = CurrentQuestion.QuestionId;
                        CurrentQuestion.AnswerItems.Add(OAnswerItem);
                    }
                }
                //doan nay save thang xml file!

                DataSet dsXmlFile = new DataSet("XmlData");
                dsXmlFile.Tables.Add("Question");
                dsXmlFile.Tables.Add("Answer");
                dsXmlFile.Tables["Answer"].Columns.Add("AnswerId");
                dsXmlFile.Tables["Answer"].Columns.Add("AnswerText");
                dsXmlFile.Tables["Answer"].Columns.Add("IsAnswer");
                dsXmlFile.Tables["Answer"].Columns.Add("QuestionId");

                dsXmlFile.Tables["Question"].Columns.Add("Id");
                dsXmlFile.Tables["Question"].Columns.Add("No");
                dsXmlFile.Tables["Question"].Columns.Add("Subject");
                for (int iIndex = 0; iIndex < Questions.Count; iIndex++)
                {
                    dsXmlFile.Tables["Question"].Rows.Add(new object[]{
                        Questions[iIndex].QuestionId,
                        Questions[iIndex].QuestionNo,
                        Questions[iIndex].Subject
                    });
                    for (int iIndexAns = 0; iIndexAns < Questions[iIndex].AnswerItems.Count; iIndexAns++)
                    {
                        dsXmlFile.Tables["Answer"].Rows.Add(new object[]{
                            Questions[iIndex].AnswerItems[iIndexAns].AnswerId,
                            Questions[iIndex].AnswerItems[iIndexAns].AnswerText,
                            Questions[iIndex].AnswerItems[iIndexAns].IsAnswer,
                            Questions[iIndex].AnswerItems[iIndexAns].QuestionId,
                        });
                    }
                }
                System.IO.File.Delete(DestFile);
                dsXmlFile.WriteXml(DestFile);
                
                WordDocument.Visible = true;
            }
            catch (Exception ex)
            {
                WordDocument.Visible = true;
                throw (ex);
            }
        }
    }
}
